package com.ruoyi.waterDistribution.controller;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.ruoyi.common.core.domain.business.waterDistribution.WiGetvolM;
import com.ruoyi.common.core.utils.bean.BeanMapUtils;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.waterDistribution.service.IWiGetvolMService;
import com.ruoyi.waterDistribution.vo.YearGetVolExcelVo;
import com.ruoyi.waterDistribution.vo.YearPlanExeclVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 取水断面月度调水水量计算Controller
 *
 * @author ps
 * @date 2023-02-16
 */
@RestController
@RequestMapping("/WiGetvolM")
public class WiGetvolMController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(WiGetvolMController.class);

    @Autowired
    private IWiGetvolMService wiGetvolMService;

    /**
     * 导入上一年数据
     */
    @GetMapping("/importLastYearData")
    public AjaxResult importLastYearData(String year,String crossCd){
        log.info("导入上一年调水数据 入参 : " + year + "=" + crossCd);
        List<WiGetvolM> list = wiGetvolMService.importLastYearData(year,crossCd);
        return AjaxResult.success(list);
    }

    /**
     * 年度调水分析 -- 显示入境取水断面的配水量和实测水量的对比
     */
    @GetMapping("/croVolAnalys")
    public AjaxResult croVolAnalys(String year, String chanCd) {
        log.info("年度调水分析 入参 : " + year + "=" + chanCd);
        HashMap<String, Object> res = wiGetvolMService.croVolAnalys(year, chanCd);
        return AjaxResult.success(res);
    }


    /**
     * 年度配水计划
     */
    @GetMapping("/getCroVolYear")
    public AjaxResult getCroVolYear(String year, String chanCd) {
        log.info("年度配水计划 入参 : " + year + "=" + chanCd);
        ArrayList<Object> resList = wiGetvolMService.getCroVolYear(year, chanCd);
        return AjaxResult.success(resList);
    }

    /**
     * 逐月调水过程 -- 汇总年度各个入境渠道取水断面的旬水量
     */
    @GetMapping("/caleCroVol")
    public AjaxResult caleCroVol(String year, String chanCd) {
        log.info("逐月调水过程 入参：" + year + "=" + chanCd);
        ArrayList<Object> res = wiGetvolMService.caleCroVol(year, chanCd);
        return AjaxResult.success(res);
    }

    /**
     * 调水过程核定 -- 汇总断面水量
     * 重新汇总取水断面旬水量
     * 根据断面下属地块算定额，结合所在渠系的利用系数算出水量 修订水量为人工修改
     */
    @GetMapping("/setCroVol")
    public AjaxResult setCroVol(String year, String crossCd) throws Exception {
        log.info("调水过程核定 入参：" + year + "=" + crossCd);
        return toAjax(wiGetvolMService.setCroVol(year, crossCd));
    }


    /**
     * 查询取水断面月度调水水量计算列表
     */
    @RequiresPermissions("distribute:WiGetvolM:list")
    @GetMapping("/list")
    public TableDataInfo list(WiGetvolM wiGetvolM) {
//        startPage();
        List<WiGetvolM> list = wiGetvolMService.selectWiGetvolMList(wiGetvolM);
        return getDataTable(list);
    }

    /**
     * 导出取水断面月度调水水量计算列表
     */
    @RequiresPermissions("distribute:WiGetvolM:export")
    @Log(title = "取水断面月度调水水量计算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, String year, String chanCd) throws Exception {
        ArrayList<Object> resList = wiGetvolMService.getCroVolYear(year, chanCd);
        List<YearGetVolExcelVo> list = new ArrayList<>();
        for (Object obj : resList) {
            YearGetVolExcelVo yearGetVolExcelVo = BeanMapUtils.mapToBean((Map<String, Object>)obj, YearGetVolExcelVo.class);
            list.add(yearGetVolExcelVo);
        }
//        List<WiGetvolM> list = wiGetvolMService.selectWiGetvolMList(wiGetvolM);
        ExcelUtil<YearGetVolExcelVo> util = new ExcelUtil<YearGetVolExcelVo>(YearGetVolExcelVo.class);
        util.exportExcel(response, list, "取水断面月度调水水量计算数据");
    }

    //导出调水过程表
    @PostMapping("/export2")
    public void export2(HttpServletResponse response, String year, String chanCd, String tableTitle) throws Exception {
        log.info("逐月用水过程导出");
        InputStream inputStream = null;
        inputStream = this.getClass().getClassLoader().getResourceAsStream("static/file/限额用水计划分配表.xlsx");
        ArrayList<Object> res = wiGetvolMService.caleCroVol(year, chanCd);
        Map<String, String> map = new HashMap<>();
        map.put("excelTitle", tableTitle);
        map.put("year", year);
        BigDecimal sum1 = new BigDecimal("0");
        BigDecimal sum2 = new BigDecimal("0");
        BigDecimal sum3 = new BigDecimal("0");
        BigDecimal yearSum = new BigDecimal("0");
        for (int i = 0; i < res.size(); i++) {
            ArrayList<String> value = (ArrayList<String>) res.get(i);
            map.put("value" + (i + 1), value.get(4));
            if (value.get(1).equals("春灌") && value.get(3).equals("月总量")) {
                sum1 = sum1.add(new BigDecimal(value.get(4)));
            }
            if (value.get(1).equals("夏灌") && value.get(3).equals("月总量")) {
                sum2 = sum2.add(new BigDecimal(value.get(4)));
            }
            if (value.get(1).equals("冬灌") && value.get(3).equals("月总量")) {
                sum3 = sum3.add(new BigDecimal(value.get(4)));
            }
            if (value.get(3).equals("月总量")) {
                yearSum = yearSum.add(new BigDecimal(value.get(4)));
            }
            map.put("sum1", sum1 + "");
            map.put("sum2", sum2 + "");
            map.put("sum3", sum3 + "");
            map.put("yearSum", yearSum + "");
        }
        ServletOutputStream out = response.getOutputStream();
        ExcelWriter excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        excelWriter.fill(map, writeSheet);
        excelWriter.finish();
    }

    /**
     * 获取取水断面月度调水水量计算详细信息
     */
    @RequiresPermissions("distribute:WiGetvolM:query")
    @GetMapping(value = "/{crossCd}")
    public AjaxResult getInfo(@PathVariable("crossCd") String crossCd) {
        return success(wiGetvolMService.selectWiGetvolMByCrossCd(crossCd));
    }

    /**
     * 新增取水断面月度调水水量计算
     */
    @RequiresPermissions("distribute:WiGetvolM:add")
    @Log(title = "取水断面月度调水水量计算", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WiGetvolM wiGetvolM) {
        return toAjax(wiGetvolMService.insertWiGetvolM(wiGetvolM));
    }

    /**
     * 修改取水断面月度调水水量计算
     */
    @RequiresPermissions("distribute:WiGetvolM:edit")
    @Log(title = "取水断面月度调水水量计算", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WiGetvolM wiGetvolM) {
        return toAjax(wiGetvolMService.updateWiGetvolM(wiGetvolM));
    }

//    /**
//     * 删除取水断面月度调水水量计算
//     */
//    @RequiresPermissions("distribute:WiGetvolM:remove")
//    @Log(title = "取水断面月度调水水量计算", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{crossCds}")
//    public AjaxResult remove(@PathVariable String[] crossCds) {
//        return toAjax(wiGetvolMService.deleteWiGetvolMByCrossCds(crossCds));
//    }
}
